package observerPattern;

import java.util.ArrayList;
import java.util.List;

//新闻机构，主题实现类
public class NewAgency implements Subject {

    private List<Observer> observers;
    private String news;

    public NewAgency() {
        observers = new ArrayList<>();
    }

    public void setNews(String news) {
        this.news = news;
        notifyObservers();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
         for (Observer observer : observers) {
             observer.update(news);
         }
    }
}
